/*Akshit Gupta Roll No-2010012
Data Structures and Algorithm Homework 1
To implement the list in python in the c using the a)ARRAYS
*/

#include<stdio.h>
/*Declarations of the functions used*/
int append( int  a[], int x,int  size);
int extend( int  a[], int b[],int  size, int sizeb);
int insert(int a[],int size);
int rem(int AR[],int SR);
int pop(int a[],int size);
int index(int a[],int s);
int b_sort(int a[],int s);
int rev(int a[] ,int s);
void print(int z[],int s);

/*Definations of the functions used*/
int rev(int a[] ,int s)
{
    int c,v;
if((s%2)==0)
{
for(c=0;c<=(s/2)-1;c++)
{
a[c]=(a[c]+a[s-c-1])-(a[s-c-1]=a[c]);   /*Swapping is done without using temp variable*/
}}
else
{
for(v=0;v<=((s-1)/2)-1;v++)
{
a[v]=(a[v]+a[s-v-1])-(a[s-v-1]=a[v]);
}}
return a,s;
}
/******************************************/
int b_sort(int a[],int s)          /*Here the bubble sorting has been done*/
{
int g,h,temp1;
for(g=0;g<s;g++)
{
for(h=0; h<s;h++)
{
if(a[h]>a[h+1])           
{
temp1=a[h];
a[h]=a[h+1];
a[h+1]=temp1;
}}}
print(a,s);
return a,s;
}
/*************************************/
int count(int a[],int s)
{
int d,flag3=0,con;
printf("Enter the element to be counted\n\n");
scanf("%d",&con);

for(d=0;d<s;d++)
{
if(a[d]==con)
{flag3++;
}}
if(flag3==0)
{
printf("ERROR:Element not found\n\n");
}
else
{
printf(" The value is found %d no. of times\n",flag3);
}                      
return a,s;
}
/*************************************/
int index(int a[],int s)
{
int y,flag2=0,ind;
printf("Enter the element to be searched\n\n");
scanf("%d",&ind);

for(y=0;y<s;y++)
{
if(a[y]==ind)
{flag2++;
ind=y;
break;
}}
if(flag2==0)
{
printf("ERROR:Element not found\n\n");
}
else
{
printf(" The value is at position %d\n",ind);
}                      
return a,s;
}
/************************************************/
int pop(int a[],int s)
{
int e,poi;
printf("Enter the position of element to be poped(removed)\n\n");
scanf("%d",&poi);
for(e=poi;e<s;e++)
{
a[e]=a[e+1];
}
a[s-1]=0;
s--;                                        /*Size is decreased as one element is deleted*/
return a,s;
}
/*************************************/
int rem(int AR[],int SR)
{
int w,rem,q,flag1=0,pod;
printf("Enter the element to be removed\n\n");
scanf("%d",&rem);
for(q=0;q<SR;q++)
{
if(AR[q]==rem)
{flag1++;
pod=q;
break;
}}
if(flag1==0)
{
printf("ERROR:Element not found\n\n");
}
for(w=pod;w<SR;w++)
{
AR[w]=AR[w+1];
}
AR[SR-1]=0;
SR--;                          
return AR,SR;
}
/*********************************************************************/
int insert( int a[] , int size)
{int u,ins,poin;
printf("Enter the element to be inserted\n\n");
scanf("%d",&ins);
printf("Enter the position where the element is to be inserted\n\n");
scanf("%d",&poin);
for(u=size;u>poin;u--)
{
a[u]=a[u-1];
}
a[poin]=ins;
size++;
return a,size;
}
/**************************************************************/
int extend( int  a[], int b[],int size ,int sizeb)
{int k,l;
for(l=0,k=size;l<sizeb,k<(size+sizeb);k++,l++)
{a[k]=b[l];
}
size=size+sizeb;
return a,size;
}
/****************************************************/
int append( int  a[], int x,int size)
{
a[size]=x;
size++;
return a,size;
}
/************************************************/
void print(int z[],int s)
{int i;
for(i=0;i<s;i++)
	{
	printf("element at pos %d is %d\n\n",i,z[i]);
	}}
int main()                                    /*Start of the main function*/
{
int *a,o,ch,size,i,x,sizeb,j;
/***********************************************************/
do
{
printf("\n\n********FUNCTIONS CHART*********\n\n");
printf("1.To add the elements in the array \n");
printf("2.To show \n");
printf("3.append() \n");
printf("4.extend() \n");
printf("5.insert() \n");
printf("6.remove() \n");
printf("7.pop() \n");
printf("8.index() \n");
printf("9.count() \n");
printf("10.sort() \n");
printf("11.reverse() \n");
printf("12.Exit \n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
     printf("Enter the size of the array\n\n");
scanf("%d",&size);
a = malloc(size* sizeof(int));                    /*Please see the use of the malloc which make it dynamic*/ 
for(i=0;i<size;i++)
{
printf("Enter the element at pos %d\n\n",i);
scanf("%d",&a[i]);
}
break;
case 2:
     	print(a,size);
break;
case 3: printf("Enter the element to be appended\n\n");
	scanf("%d",&x);

	a,size=append(a,x,size);
	printf("Size of the array==%d\n",size);
	break;
 
case 4 :printf("Enter the size of array to be extended\n\n");
	scanf("%d",&sizeb);
	int *b=malloc(sizeof(int)*sizeb);                          /*Allocating the array to be extended*/
	for(j=0;j<sizeb;j++)
	{
	printf("Enter the element at pos %d\n\n",j);
	scanf("%d",&b[j]);
	}
    a,size=extend(a,b,size,sizeb);
    break;

case 5: a,size=insert(a,size);
	 break;
case 6: a,size=rem(a,size);
     break;
case 7: a,size=pop(a,size);
	break;
case 8 :a,size=index(a,size);
	break;
case 9 :a,size=count(a,size);
    break;
case 10: a,size=b_sort(a,size);
    break;

case 11: a,size=rev(a,size);
	
	break;
case 12:printf("\n\n\nBYE BYE!!!!!\n\n");
	printf("Have a nice day..:\n\n");
	break;
default:printf("Wrong choice entered\n\n");
	break;

}}while(ch!=12);	
return 0;
}
/*End of the main function*/
